package com.homework.loans;

import io.dropwizard.hibernate.AbstractDAO;
import org.hibernate.SessionFactory;

import java.util.Collections;
import java.util.List;

import static java.util.Optional.ofNullable;

/**
 * Provides customer CRUD operations.
 *
 * @author Blind
 */
public class CustomerDao extends AbstractDAO<Customer> {
    public CustomerDao(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    public Customer createOrUpdate(Customer customer) {
        return persist(customer);
    }

    public List<Loan> loans(int customerId) {
        return ofNullable(get(customerId)).map(c -> c.loans).orElse(Collections.<Loan>emptyList());
    }
}
